﻿$(function() {
    var initialize = function(filterbar) {
        filterbar = $(filterbar);
        var filterbarLink = filterbar.find("li.option a.filter");
        var filteraddlink = filterbar.find("li.option.add a.link");

        // Clicking on a filter should show the editor.
        filterbarLink.click(function() {
            var jthis = $(this);

            jthis.find(".display").hide();
            jthis.find(".editor").show();
        });

        // Clicking on the add filter button should show the popup.
        filteraddlink.click(function() {
            var jthis = $(this);

            var availablefilters = $(filterbar).find("div.available-filters");
            if (availablefilters.is(":visible")) {
                availablefilters.hide();
            } else {
                availablefilters.show();
            }
        });
    };

    var filterbars = $("div.filterbar");
    for (var i = 0; i < filterbars.length; i++) {
        initialize(filterbars[i]);
    }
});